import 'package:flutter_crud_test_amir_rabbani/core/params/create_customer_params.dart';
import 'package:flutter_crud_test_amir_rabbani/core/resource/data_state.dart';
import 'package:flutter_crud_test_amir_rabbani/core/resource/use_case.dart';
import 'package:flutter_crud_test_amir_rabbani/domain/usecases/create_customer_usecase.dart';
import 'package:flutter_crud_test_amir_rabbani/domain/usecases/delete_customer_usecase.dart';
import 'package:flutter_crud_test_amir_rabbani/domain/usecases/edit_customer_usecase.dart';
import 'package:flutter_crud_test_amir_rabbani/domain/usecases/read_by_id_customer_usecase.dart';
import 'package:flutter_crud_test_amir_rabbani/domain/usecases/read_customer_usecase.dart';
import 'package:flutter_crud_test_amir_rabbani/routes/app_pages.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class CrudController extends GetxController {
  CreateCustomerUseCase _createCustomerUseCase;
  DeleteCustomerUseCase _deleteCustomerUseCase;
  EditCustomerUseCase _editCustomerUseCase;
  ReadByIdCustomerUseCase _byIdCustomerUseCase;
  ReadCustomerUseCase _readCustomerUseCase;

  CrudController(
      this._createCustomerUseCase,
      this._deleteCustomerUseCase,
      this._editCustomerUseCase,
      this._byIdCustomerUseCase,
      this._readCustomerUseCase);

  @override
  void onInit() {
    super.onInit();
  }

  read() => _readCustomerUseCase(NoParams());

  delete(int id) async {
    DataState dataState = await _deleteCustomerUseCase(id);
    if (dataState is DataSuccess) {
      EasyLoading.showToast('Success');
    } else {
      EasyLoading.showToast(dataState.error ?? 'Error');
    }
  }

  void addClick() {
    Get.toNamed(Routes.ADD_CUSTOMER);
  }

  void editClick(int id) {
    Get.toNamed(Routes.ADD_CUSTOMER, parameters: {'id': id.toString()});
  }
}
